package com.jiaoyou.baojian.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.entity.CardIndex;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.http.response.BaseResponse;
import com.jiaoyou.baojian.otto.Event;
import com.squareup.otto.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

public class WalletActivity extends BaseActivity {

    @BindView(R.id.yue)
    TextView yue;
    @BindView(R.id.chongzhi)
    Button chongzhi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);

        setTitle("钱包");

        refresh();

        needOtto();
    }

    @Subscribe
    public void onEvent(String event){
        if(Event.EVENT_ALIPAY.equals(event)){
            Log.i("EVENT_ALIPAY", "支付宝充值成功");
            refresh();
        }
    }

    private void refresh() {
        Api.getInstance().getCardIndex(new BaofangCallback<CardIndex>(){
            @Override
            public void onFailure(Call<BaseResponse<CardIndex>> call, Throwable t) {
                toast("获取钱包信息失败，请重试");
                onBackPressed();
            }

            @Override
            public void onSuccess(CardIndex response) {
                yue.setText(response.amount + "豆");
            }
        });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_wallet;
    }

    @OnClick({R.id.chongzhi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chongzhi:
                RechargeActivity.show(this);
                break;
        }
    }
}
